package com.itxckf.controller;

import com.itxckf.pojo.Result;
import com.itxckf.pojo.RolePath;
import com.itxckf.service.RoleService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;
    @PostMapping("/getRole")
    public Result getRole(){
        return Result.success(roleService.getRole());
    };

//    通过用户id 获取 权限路径
    @PostMapping("/getUserRole")
    public Result getUserRole(@RequestParam Short id){
        List<RolePath> rolePaths = roleService.getUserRole(id);
        return Result.success(rolePaths);
    };
    @PostMapping("/editUserRole")
    public Result editUserRole(@RequestBody EditUserRoleForm editUserRoleForm){
        log.info("分配权限，{}",editUserRoleForm);
        roleService.editUserRole(editUserRoleForm.getRoleID(),editUserRoleForm.getNewIds());
        return Result.success("分配成功！");
    };




    /**
     * 查询所有的路径
     * @return
     */
    @PostMapping("/getPaths")
    public Result getPaths(){
        List<RolePath> rolePaths = roleService.getPaths();
        return Result.success(rolePaths);
    };

    /**
     * 添加路径
     * @param rolePath
     * @return
     */
    @PostMapping("/addPath")
    public Result addPath(@RequestBody RolePath rolePath){
        log.info("添加路径 ，{}",rolePath);
        roleService.addPath(rolePath);
        return Result.success("添加成功！");
    };

    @PostMapping("/deletePath")
    public Result deletePath(@RequestParam Short id){
        log.info("删除路径 ，{}",id);
        roleService.deletePath(id);
        return Result.success("删除成功！");
    };

    @PostMapping("/editPath")
    public Result editPath(@RequestBody RolePath rolePath){
        log.info("编辑路径 ，{}",rolePath);
        roleService.editPath(rolePath);
        return Result.success("编辑成功！");
    };
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class EditUserRoleForm {
    private Short roleID;
    private List<Short> newIds;
}
